package com.yang.service.Impl;


import com.yang.domain.Result;
import com.yang.domain.VO.AdminUserInfoVo;
import com.yang.domain.VO.RoutersVo;
import com.yang.domain.VO.UserInfoVo;
import com.yang.domain.entity.LoginUser;
import com.yang.domain.entity.Menu;
import com.yang.domain.entity.User;
import com.yang.service.MenuService;
import com.yang.service.RoleService;
import com.yang.service.SystemLoginService;
import com.yang.utils.BeanCopyUtils;
import com.yang.utils.JwtUtil;
import com.yang.utils.RedisCache;
import com.yang.utils.SecurityUtils;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.yang.constants.RedisConstants.SYSTEM_LOGIN;

/**
 * <p>
 * 后台登录服务层实现类
 * </p>
 * @author yang
 * @date 2023/5/29
 */
@Service
public class SystemLoginServiceImpl implements SystemLoginService {


    @Resource
    private RedisCache redisCache;

    @Resource
    private AuthenticationManager authenticationManager;

    @Resource
    private MenuService menuService;

    @Resource
    private RoleService roleService;


    @Override
    public Result login(User user) {
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(user.getUserName(),user.getPassword());

        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        //判断是否认证通过
        if(Objects.isNull(authenticate)){
            throw new RuntimeException("用户名或密码错误");
        }
        //获取userid 生成token
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        String userId = loginUser.getUser().getId().toString();
        String jwt = JwtUtil.createJWT(userId);

        //把用户信息存入redis
        redisCache.setCacheObject(SYSTEM_LOGIN + userId,loginUser);

        //把token封装 返回
        Map<String,String> map = new HashMap<>();
        map.put("token",jwt);
        return Result.okResult(map);
    }


    @Override
    public Result logout() {
        //获取当前登录的用户id
        Long userId = SecurityUtils.getUserId();
        //删除redis中对应的值
        redisCache.deleteObject(SYSTEM_LOGIN + userId);

        return Result.okResult();
    }

    @Override
    public Result getInfo() {
        //1.获取当前登录的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        User user = loginUser.getUser();
        Long userId = loginUser.getUser().getId();

        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVo.class);

        //2.获取这个用户的权限信息
        List<String> permissions = menuService.selectPermsByUserId(userId);

        //3.获取这个用户的角色信息
        List<String> roles = roleService.selectRoleKeyByUserId(userId);

        //4.将这些数据进行封装
        AdminUserInfoVo adminUserInfoVo = new AdminUserInfoVo(permissions,roles,userInfoVo);

        //5.返回数据
        return Result.okResult(adminUserInfoVo);
    }

    @Override
    public Result getRouters() {
        //1.获取当前登录的用户
        Long userId = SecurityUtils.getUserId();

        //2.查询menu 结果是tree的形式
        List<Menu> menus = menuService.selectRouterMenuTreeByUserId(userId);

        //3.封装数据返回
        return Result.okResult(new RoutersVo(menus));
    }


}
